/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.test.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaaction.common.persistence.Page;
import com.kaaction.common.service.CrudService;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.test.entity.MmDad;
import com.kaaction.modules.test.dao.MmDadDao;
import com.kaaction.modules.test.entity.MmSon;
import com.kaaction.modules.test.dao.MmSonDao;

/**
 * testService
 * @author test
 * @version 2017-04-26
 */
@Service
@Transactional(readOnly = true)
public class MmDadService extends CrudService<MmDadDao, MmDad> {

	@Autowired
	private MmSonDao mmSonDao;
	
	public MmDad get(String id) {
		MmDad mmDad = super.get(id);
		mmDad.setMmSonList(mmSonDao.findList(new MmSon(mmDad)));
		return mmDad;
	}
	
	public List<MmDad> findList(MmDad mmDad) {
		return super.findList(mmDad);
	}
	
	public Page<MmDad> findPage(Page<MmDad> page, MmDad mmDad) {
		return super.findPage(page, mmDad);
	}
	
	@Transactional(readOnly = false)
	public void save(MmDad mmDad) {
		super.save(mmDad);
		for (MmSon mmSon : mmDad.getMmSonList()){
			if (mmSon.getId() == null){
				continue;
			}
			if (MmSon.DEL_FLAG_NORMAL.equals(mmSon.getDelFlag())){
				if (StringUtils.isBlank(mmSon.getId())){
					mmSon.setDadId(mmDad);
					mmSon.preInsert();
					mmSonDao.insert(mmSon);
				}else{
					mmSon.preUpdate();
					mmSonDao.update(mmSon);
				}
			}else{
				mmSonDao.delete(mmSon);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(MmDad mmDad) {
		super.delete(mmDad);
		mmSonDao.delete(new MmSon(mmDad));
	}
	
}